package com.doujiao.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiao.auth.dto.AdminLogPageDTO;
import com.doujiao.auth.mapper.AdminLogMapper;
import com.doujiao.auth.service.AdminLogService;
import com.doujiao.core.auth.entity.AdminLog;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 管理员日志服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2025-05-24
 */
@Service
public class AdminLogServiceImpl extends ServiceImpl<AdminLogMapper, AdminLog> implements AdminLogService {

    /**
     * 分页查询管理员日志
     *
     * @param dto 查询参数
     * @return 分页结果
     */
    @Override
    public IPage<AdminLog> page(AdminLogPageDTO dto) {
        // 创建分页对象
        Page<AdminLog> page = new Page<>(dto.getPageNum(), dto.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<AdminLog> queryWrapper = new LambdaQueryWrapper<>();

        // 管理员名称条件
        if (StringUtils.hasText(dto.getAdminName())) {
            queryWrapper.like(AdminLog::getAdminName, dto.getAdminName());
        }

        // 操作内容条件
        if (StringUtils.hasText(dto.getContent())) {
            queryWrapper.like(AdminLog::getContent, dto.getContent());
        }

        // 时间范围条件
        if (StringUtils.hasText(dto.getStartTime())) {
            queryWrapper.ge(AdminLog::getCreateTime, dto.getStartTime());
        }

        if (StringUtils.hasText(dto.getEndTime())) {
            queryWrapper.le(AdminLog::getCreateTime, dto.getEndTime());
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(AdminLog::getCreateTime);

        // 执行分页查询
        return page(page, queryWrapper);
    }
}
